package com.exam.paper.service;

import com.exam.common.entity.PageRequest;
import com.exam.paper.mapper.SelectMapper;
import com.exam.paper.mapper.WrongMapper;
import com.exam.paper.pojo.Paper;
import com.exam.paper.pojo.Select;
import com.exam.paper.pojo.Wrong;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : WrongService
 * @Description : 错题本
 * @Author : WHB
 */
@Service
public class WrongService {

    @Autowired
    WrongMapper wrongMapper;

    @Autowired
    SelectMapper selectMapper;

    public void addWrong(Long uid, Long sid) {
        Wrong wrong = new Wrong();
        wrong.setUid(uid);
        wrong.setSid(sid);
        wrong.setCreat_time(new Date());
        wrongMapper.insert(wrong);
    }

    public void deleteWrong(Long id) {
        Wrong wrong = new Wrong();
        wrong.setId(id);
        wrongMapper.delete(wrong);
    }

    public PageRequest<Select> queryWrongList(Long uid, Integer page, Integer size) {

        //启动分页
        PageHelper.startPage(page,size);

        List<Select> selectList = new ArrayList<>();
        Wrong wrong = new Wrong();
        wrong.setUid(uid);
        List<Wrong> wrongList = wrongMapper.select(wrong);
        //循环获取错题的sid
        for (Wrong w:
             wrongList) {
            //查找题目信息
            Select select = new Select();
            select.setId(w.getSid());
            selectList.add(selectMapper.selectOne(select));
        }

        //解析分页结果
        PageInfo<Select> info = new PageInfo<>(selectList);

        return new PageRequest<Select>(info.getTotal(),selectList);
    }
}
